package com.app.qbake.module.mine.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.wind.base.adapter.BaseRecyclerAdapter;
import com.wind.base.utils.ViewInflater;
import com.wind.data.mine.bean.Address;
import com.app.qbake.R;
import com.app.qbake.module.mine.activity.EditAddressActivity;
import com.app.qbake.module.mine.layout.EditAddressLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AddressAdapter extends BaseRecyclerAdapter<Address,AddressAdapter.ViewHolder> {

    private EditAddressLayout mEditAddressLayout;
    public AddressAdapter(Activity activity, int layoutRes) {
        super(activity, layoutRes);
        mEditAddressLayout=new EditAddressLayout(activity);
    }

    @Override
    public ViewHolder onCreateViewHolder(View v) {
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Address address=getItem(position);
        ViewInflater.inflateText(holder.tv_name,address.getName());
        ViewInflater.inflateText(holder.tv_phone,address.getPhone());
        final String addr= address.getProvince()+address.getCity()+address.getAddress();
        ViewInflater.inflateText(holder.tv_address,addr);
        if (address.getIs_default()==1){
            holder.ll_default.setActivated(true);
        }else {
            holder.ll_default.setActivated(false);
        }
        holder.tv_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        holder.tv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (address.getIs_default()==0){

                }
            }
        });

        holder.ll_default.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (address.getIs_default()==0) {
                    //设置为默认地址
                    address.setIs_default(1);
                    mEditAddressLayout.edit(address);
                }
            }
        });

        holder.tv_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditAddressActivity.start(mActivity,address);
            }
        });
        holder.tv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mEditAddressLayout.del(address);
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_phone)
        TextView tv_phone;
        @BindView(R.id.tv_address)
        TextView tv_address;
        @BindView(R.id.ll_default)
        View ll_default;
        @BindView(R.id.tv_del)
        TextView tv_del;
        @BindView(R.id.tv_edit)
        TextView tv_edit;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
